package myutil;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class HttpUrlDome {



    private static String AGE_URL = "http://tupapi.xfyun.cn/v1/age";
    //   private static String APE_URL = "http://tupapi.xfyun.cn/v1/face_score";
    private static String APPID = "d1d5cb9f";
    private static String API_KEY = "59f015b6e947286c0d0df08e47d995e7";

    //    public static String handleHttpUrlRequest(byte[] img) {
//        try {
//            //1.构造一个URL对象
//            URL httpUrl = new URL(AGE_URL);
//            //2.开启连接
//            HttpURLConnection connection = (HttpURLConnection)httpUrl.openConnection();
//            //3.设置请求头以及签名
//            String curTime = System.currentTimeMillis() / 1000 + ""; //当前UTC时间戳
//            String imageName = "feature.jpg";
//            String param = "{\"image_name\":\"" + imageName + "\"}";
//            String paramBase64 = new String(Base64.encodeBase64(param.getBytes("UTF-8")));
//            String checkSum = DigestUtils.md5Hex(API_KEY + curTime + paramBase64);
//            connection.setRequestProperty("X-Appid",APPID);//APPID
//            connection.setRequestProperty("X-CurTime",curTime);//当前UTC时间戳 单位秒
//            connection.setRequestProperty("X-Param",paramBase64);//业务参数，需要经过base64编码
//            connection.setRequestProperty("X-CheckSum",checkSum);//令牌，身份
//            //4.设置请求体
//            connection.setDoOutput(true);
//            connection.setDoInput(true);
//            connection.setRequestProperty("Content-Type","binary/octet-stream");//文件类型二进制字节文件流
//            OutputStream out = connection.getOutputStream();
//            out.write(img);
//            out.flush();
//            out.close();
//            //5.发起请求，获取响应内容
//            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//            String line;
//            String result = "";
//            while ((line =in.readLine()) != null)
//            {
//                result += line;
//            }
//            //控制台响应内容
//            System.out.println(result);
//            return result;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "";
//        }
//    }
    public static String handleHttpRequest(String url, Map<String, String> header, byte[] imgs) {
        try {
            URL connectionUrl = new URL(url);
            // 2、与目标地址建立连接
            HttpURLConnection urlConnection = (HttpURLConnection) connectionUrl.openConnection();


            for (String key : header.keySet()) {
                urlConnection.setRequestProperty(key, header.get(key));
            }

            //4请求体
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setRequestProperty("Content-Type", "binary/octet-stream");
            OutputStream out = urlConnection.getOutputStream();
            out.write(imgs);
            out.flush();
            out.close();
            //5发起请求
            /*InputStream inputStream = urlConnection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);*/

            if (HttpURLConnection.HTTP_OK != urlConnection.getResponseCode()) {
                System.out.println("Http 请求失败，状态码：" + urlConnection.getResponseCode());
                return null;
            }
            BufferedReader in = null;
            // 获取响应body
            in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String line;
            String result = "";
            /*while ((line = bufferedReader.readLine()) != null) {
                result += line;
            }*/

            while ((line = in.readLine()) != null) {
                result += line;
            }

            System.out.println(result);//传入的
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}